package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public final class RoomLabelAdapter extends AppAdapter<String> {

    private Map<String, String> selected = new LinkedHashMap<>();
    public RoomLabelAdapter(Context context) {
        super(context);
        selected.put("客厅", "客厅");
        selected.put("主卧", "主卧");
        selected.put("餐厅", "餐厅");
        selected.put("厨房", "厨房");
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mTextView;

        private ViewHolder() {
            super(R.layout.room_label_item);
            mTextView = findViewById(R.id.tv_room);
            mTextView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            view.setSelected(!view.isSelected());
            String data = (String) view.getTag();
            if (view.isSelected()) {
                selected.put(data, data);
            } else {
                selected.remove(data);
            }
            super.onClick(view);
        }

        @Override
        public void onBindView(int position) {
            mTextView.setText(getItem(position));
            mTextView.setTag(getItem(position));
            if(selected.get(getItem(position)) != null){
                mTextView.setSelected(true);
            }else{
                mTextView.setSelected(false);
            }
        }
    }

    public ArrayList<String> getSelectedData() {
        ArrayList<String> checkList = new ArrayList<>();
        for(String key : selected.keySet()){
            checkList.add(selected.get(key));
        }
        return checkList;
    }

    public void checkSelected(List<String> selected){
        if(selected != null){
            for(String data : selected){
                this.selected.put(data, data);
            }
        }
        notifyDataSetChanged();
    }

    public void setSelected(List<String> selected){
        if(selected != null){
            for(String data : selected){
                this.selected.put(data, data);
            }
        }
    }

    public void clearSelected(){
        selected.clear();
    }
}